package findWords;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Solution {
    public String[] findWords(String[] words) {
        String first = "qwertyuiop";
        String second = "asdfghjkl";
        String third = "zxcvbnm";
        Map<Character,Integer>map=new HashMap<>();
        for (int i = 0; i < first.length(); i++) {
            map.put(first.charAt(i),1);
            map.put((char)(first.charAt(i)+'A'-'a'),1);
        }
        for (int i = 0; i < second.length(); i++) {
            map.put(second.charAt(i),2);
            map.put((char)(second.charAt(i)+'A'-'a'),2);
        }
        for (int i = 0; i < third.length(); i++) {
            map.put(third.charAt(i),3);
            map.put((char)(third.charAt(i)+'A'-'a'),3);
        }
        ArrayList<String>list = new ArrayList<>();
        for (int i = 0; i < words.length; i++) {
            boolean canAdd = true;
            for (int j = 0; j+1 < words[i].length(); j++) {
                if(map.get(words[i].charAt(j))!=map.get(words[i].charAt(j+1))){
                    canAdd = false;
                    break;
                }
            }
            if(canAdd){
                list.add(words[i]);
            }
        }
        return list.toArray(new String[list.size()]);
    }
}
